package org.zachary.agent.infrastructure.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.zachary.agent.domain.aggregate_model.token.RedisTokenEntity;


@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, RedisTokenEntity> tokenRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, RedisTokenEntity> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // Key序列化为字符串
        template.setKeySerializer(new StringRedisSerializer());

        // Value使用FastJSON2序列化器
        template.setValueSerializer(new FastJson2RedisSerializer<>(RedisTokenEntity.class));

        template.afterPropertiesSet();
        return template;
    }
    @Bean
    public RedisTemplate<String, Object> genericRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new FastJson2RedisSerializer<>(Object.class));
        template.afterPropertiesSet();
        return template;
    }
}
